package com.xuecheng.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.auth.config.RedisUtils;
import com.xuecheng.ucenter.mapper.Iuser;
import com.xuecheng.ucenter.mapper.UserMapper;
import com.xuecheng.ucenter.model.dto.AuthParamsDto;
import com.xuecheng.ucenter.model.po.User;
import com.xuecheng.ucenter.model.po.Users;
import com.xuecheng.ucenter.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("sms_authservice")
public class PhoneSeverImpl implements AuthService {
    @Autowired
    @Resource
    private RedisUtils redisUtils;
    @Autowired
    Iuser usera;
    @Autowired
    UserMapper userMapper;


    @Override
    public Users execute(AuthParamsDto authParamsDto) {
        String cellphone = authParamsDto.getCellphone();
        String phonekey = authParamsDto.getPhonekey();
        String string = redisUtils.get(cellphone).toString();

        //判断验证码是否正确
        boolean equals1 = string.equals(phonekey);

        if (!equals1){
            throw new RuntimeException("验证码输入错误");
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",cellphone);
        User user2 = usera.selectOne(wrapper);
        Users user = userMapper.selectUserById(user2.getUsername());
        System.out.println(user);
        if(user==null){
            throw new RuntimeException("账号不存在");
        }
        return user;
    }
}
